//
//  DEWBKInforViewController.m
//  OtherDemo
//
//  Created by kangbinbin on 2018/7/28.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWBKInforViewController.h"
#import "UIBarButtonItem+MainBar.h"
#import "DEWBKInfoHeaderCell.h"
#import "DEWBKInfoCell.h"
#import "DEWAnswerView.h"
#import "FDActionSheet.h"

static NSString *const infoHeaderCellId = @"infoheader";
static NSString *const infocell = @"infocell";

@interface DEWBKInforViewController ()<UITableViewDataSource,UITableViewDelegate,FDActionSheetDelegate>
/**  */
@property (strong, nonatomic) UITableView *tableView;
/** 我要回答的View */
@property (strong, nonatomic) DEWAnswerView *answerView;
/** 分享View */
@property (strong, nonatomic) FDActionSheet *sheet;


@end

@implementation DEWBKInforViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self createView];
    
}

#pragma mark ----RequestData
-(void)loadData
{
    
    
    
    
}


#pragma mark ----initView
-(void)createView
{
    self.navigationItem.title = @"百科详情";
    
    UIBarButtonItem *rightItem = [UIBarButtonItem itemWithTarget:self action:@selector(rightBarButtonItemClick) image:@"分享" highImage:@"分享" andTitle:nil];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self creatTab];
    
    [self creatBottomView];
    ///监听键盘将要出现时
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    //监听当键将要退出时
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    // 1.键盘弹出需要的时间
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        //获取键盘的高度
        NSDictionary *userInfo = [notification userInfo];
        NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [value CGRectValue];
        int height = keyboardRect.size.height;
        self.answerView.y = DEWScreenHeight - NAVH - 180 - height;
        NSLog(@"%d",height);
    }];
}

//当键退出
- (void)keyboardWillHide:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    int height = keyboardRect.size.height;
    
    self.answerView.y = DEWScreenHeight - NAVH - 180;
    
}
#pragma mark - 创建底部按钮
-(void)creatBottomView{
    NSLog(@"%f",TabBarHeight);
    UIView *BottomView  = [[UIView alloc]initWithFrame:CGRectMake(0, DEWScreenHeight - TabBarHeight  - NAVH - 10, DEWScreenWidth  , 60)];
    BottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:BottomView];
    
    UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    answerBtn.frame = CGRectMake(15, 10, DEWScreenWidth - 30, 40);
    [answerBtn setTitle:@"我要回答" forState:UIControlStateNormal];
    [answerBtn setBackgroundColor:UIColorFromRGB(0xFF8900)];
    answerBtn.layer.cornerRadius = 4;
    [answerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [answerBtn addTarget:self action:@selector(answeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [BottomView addSubview:answerBtn];
    
}
#pragma mark - 我要回答按钮点击事件
- (void)answeBtnClick:(UIButton *)sender{
    [self.view addSubview:self.answerView];
    
}



#pragma mark - 导航栏按钮点击事件
-(void)rightBarButtonItemClick{
    NSLog(@"点击分享");
    [self.view addSubview:self.sheet];
    [self.sheet show];
    
    
}
-(void)creatTab{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEWScreenWidth , DEWScreenHeight - NAVH) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    if (@available(iOS 11.0, *)){
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DEWBKInfoHeaderCell class]) bundle:nil] forCellReuseIdentifier:infoHeaderCellId];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DEWBKInfoCell class]) bundle:nil] forCellReuseIdentifier:infocell];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return 20;
    }
   
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        DEWBKInfoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:infoHeaderCellId];
        
        return cell;
    }else{
        DEWBKInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:infocell];
        
        return cell;
        
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 320;
    }else{
        return 500;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //解除键出现盘通知；
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    //解除键盘隐藏通知;
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
#pragma mark - FDActionSheetDelegate
-(void)actionSheetCancel:(FDActionSheet *)actionSheet{
    NSLog(@"点击了取消按钮");
}
-(void)actionSheet:(FDActionSheet *)sheet clickedButtonIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",buttonIndex);
    if (buttonIndex == 0 ) {//收藏
        
    }else if (buttonIndex == 1){//分享
        
    }
    else{//举报
        
    }
}
#pragma mark - layze
-(void)dealloc{
    //解除键出现盘通知；
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    //解除键盘隐藏通知;
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];

}
-(DEWAnswerView *)answerView{
    if (_answerView == nil) {
        
        _answerView = [[[NSBundle mainBundle] loadNibNamed:@"DEWAnswerView" owner:self options:nil] lastObject];
        _answerView.frame = CGRectMake(0, DEWScreenHeight - NAVH - 180, DEWScreenWidth, 180);
    }else{
        [_answerView show];
    }
    return _answerView;
}
-(FDActionSheet *)sheet{
    if (_sheet == nil) {
        _sheet = [[FDActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"收藏",@"分享",@"举报", nil];
        _sheet.delegate = self;
    }
    return _sheet;
}
@end
